﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CopticCalendarUnitTest
{
    public static class AssertExtensions
    {
        public static void Throws<T>(Action code) where T : Exception
        {
            Assert.IsInstanceOfType(GetException(code), typeof(T));
        }

        public static void Throws<T>(Action code, string message) where T : Exception
        {
            Assert.IsInstanceOfType(GetException(code), typeof(T), message);
        }

        public static void ThrowsMessage<T>(Action code, string expectedMessage) where T : Exception
        {
            Exception e = GetException(code);
            Assert.IsInstanceOfType(e, typeof(T));
            Assert.AreEqual(e.Message, expectedMessage);
        }

        public static void ThrowsMessage<T>(Action code, string expectedMessage, string message) where T : Exception
        {
            Exception e = GetException(code);
            Assert.IsInstanceOfType(e, typeof(T), message);
            Assert.AreEqual(e.Message, expectedMessage, message);
        }

        public static void ThrowsNothing(Action code)
        {
            Assert.IsNull(GetException(code));
        }

        public static void ThrowsNothing(Action code, string message)
        {
            Assert.IsNull(GetException(code), message);
        }

        private static Exception GetException(Action action)
        {
            try
            {
                action.Invoke();
                return null;
            }
            catch (Exception exception)
            {
                return exception;
            }
        }
    }
}
